import { createRouter, createWebHistory } from 'vue-router'

// 路由懒加载
const dataPreprocess = () => import('@/views/dataPreprocess.vue')
const textAlign = () => import('@/views/textAlign.vue')
const textRetrieve = () => import('@/views/textRetrieve.vue')
const dataStats = () => import('@/views/dataStats.vue')
const userRegister = () => import('@/views/userRegister.vue')
const login = () => import('@/views/user/useLogin.vue')
const llm = () => import('@/views/llmProcess.vue')
const adminDashboard = () => import('@/views/admin/AdminDashboard.vue')
const favorites = () => import('@/views/Favorites.vue')
const historyView = () => import('@/views/user/UserHistory.vue')
const userProfile = () => import('@/views/user/UserProfile.vue')
const userHome = () => import('@/views/user/UserHome.vue')
const featuresDemo = () => import('@/views/FeaturesDemo.vue')
const aboutSystem = () => import('@/views/AboutSystem.vue')

const routes = [
  {
    path: '/',
    name: 'Home',
    redirect: '/user-home'
  },
  {
    path: '/user-home',
    name: 'UserHome',
    component: userHome,
    meta: {
      title: '主页',
      requiresAuth: true
    }
  },
  {
    path: '/features',
    name: 'FeaturesDemo',
    component: featuresDemo,
    meta: {
      title: '功能演示',
      requiresAuth: true
    }
  },
  {
    path: '/about',
    name: 'AboutSystem',
    component: aboutSystem,
    meta: {
      title: '关于系统',
      requiresAuth: true
    }
  },
  {
    path: '/profile',
    name: 'UserProfile',
    component: userProfile,
    meta: {
      title: '个人中心',
      requiresAuth: true
    }
  },
  {
    path: '/history',
    name: 'History',
    component: historyView,
    meta: {
      title: '历史记录'
    }
  },
  {
    path: '/preprocess',
    name: 'DataPreprocess',
    component: dataPreprocess,
    meta: {
      title: '数据预处理'
    }
  },
  {
    path: '/align',
    name: 'TextAlign',
    component: textAlign,
    meta: {
      title: '文本对齐'
    }
  },
  {
    path: '/retrieve',
    name: 'TextRetrieve',
    component: textRetrieve,
    meta: {
      title: '文本检索'
    }
  },
  {
    path: '/register',
    name: 'UserRegister',
    component: userRegister,
    meta: {
      title: '用户注册'
    }
  },
  {
    path: '/login',
    name: 'UserLogin',
    component: login,
    meta: {
      title: '登录'
    }
  },
  {
    path: '/llmprocess',
    name: 'LLMProcess',
    component: llm,
    meta: {
      title: '大语言模型'
    }
  },
  {
    path: '/stats',
    name: 'DataStats',
    component: dataStats,
    meta: {
      title: '数据统计'
    }
  },
  {
    path: '/favorites',
    name: 'Favorites',
    component: favorites,
    meta: {
      title: '我的收藏',
      requiresAuth: true
    }
  },
  {
    path: '/admin/dashboard',
    name: 'AdminDashboard',
    component: adminDashboard,
    meta: {
      title: '管理员后台',
      requiresAuth: true,
      requiresAdmin: true
    }
  },
  // 404页面
  {
    path: '/:pathMatch(.*)*',
    name: 'notFound',
    redirect: '/preprocess'
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 面向东盟的多语言文本对齐系统` : '面向东盟的多语言文本对齐系统'
  
  // 检查是否需要认证
  if (to.meta.requiresAuth) {
    const token = localStorage.getItem('token')
    const user = JSON.parse(localStorage.getItem('user') || '{}')
    
    if (!token || !user.id) {
      // 未登录，跳转到登录页
      next('/login')
      return
    }
    
    // 检查是否需要管理员权限
    if (to.meta.requiresAdmin && user.role !== 'admin') {
      // 非管理员，跳转到首页
      next('/')
      return
    }
  }
  
  next()
})

export default router